package com.example.controller;

import com.example.entity.po.Student;
import com.example.utils.ResultUtils;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/student")
    @ResponseBody
    public ResultUtils studentList(){
        List<Student> students = studentService.queryStudent();
        if (students.size()==0){
            return ResultUtils.Failure("获取学生信息失败", null);
        }else {
            return ResultUtils.Successful("获取学生信息成功",students);
        }
    }

    @PostMapping("/deleteStudent")
    @ResponseBody
    public ResultUtils deleteStudent(String studentId){
        int res= studentService.deleteStudent(studentId);
        if (res==0){
            return ResultUtils.Failure("删除学生信息失败",null);
        }else {
            return ResultUtils.Successful("删除学生信息成功",null);
        }
    }

    @PostMapping("/updateStudent")
    @ResponseBody
    public ResultUtils updateStudent(@RequestBody Student student){
        int res= studentService.updateStudent(student);
        if (res==0){
            return ResultUtils.Failure("修改失败",null);
        }else {
            return ResultUtils.Successful("修改成功",null);
        }
    }

    @PostMapping("/addStudent")
    @ResponseBody
    public ResultUtils addStudent(@RequestBody Student student){
        int res= studentService.addStudent(student);
        if (res==0){
            return ResultUtils.Failure("添加失败输入学号错误",null);
        }else {
            return ResultUtils.Successful("添加成功",null);
        }
    }

    @PostMapping("/fuzzyStudent")
    public ResultUtils fuzzyStudent(String studentName){
        System.out.println(studentName);
        List<Student> students= studentService.fuzzyStudent(studentName);
        if (students.size()==0){
            return ResultUtils.Failure("搜索失败或无该学生",null);
        }else {
            return ResultUtils.Successful("搜索学生成功",students);
        }
    }

//    @PostMapping("/checkStudent")
//    public ResultUtils checkStudent(String studentName){
//        List<Student> students = studentService.checkStudent(studentName);
//        if (students.size()==0){
//            return ResultUtils.Failure("无该学生",null);
//        }else {
//            return ResultUtils.Successful("搜索学生成功",students);
//        }
//    }
}
